﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DicomLibrary
{
    public class DFile
    {
        private List<DDataElement> MetaInformation = new List<DDataElement>();
        public DTransferSyntax TransferSyntax
        {
            get
            {
                switch ((string)MetaInformation.GetDataElement(0x0002, 0x0010).Value)
                {
                    case "1.2.840.10008.1.2":
                        return DTransferSyntax.ImplicitLittleEndian;
                    case "1.2.840.10008.1.2.1":
                        return DTransferSyntax.ExplicitLittleEndian;
                    case "1.2.840.10008.1.2.2":
                        //return TransferSyntax.ExplicitBigEndian;
                    default:
                        throw new NotImplementedException();
                }
            }
        }

        public List<DDataElement> DataElements = new List<DDataElement>();

        public static DFile Load(string fileName)
        {
                return DFile.Load(new FileStream(fileName, FileMode.Open));
        }
        public static DFile Load(Stream inputStream)
        {
            if (inputStream == null)
                throw new ArgumentNullException("inputStream");

            //I'm not quite sure yet what to do with IO exceptions
            //My idea is now to just let the go becuase it's in the Load method of the function
            BinaryReader reader = new BinaryReader(inputStream);
            

            //Checking if file is a valid Dicom File
            //However!!! not every file should have a preamble and prefix
            //It's currently not implemented because most have it so far
            reader.ReadBytes(128);
            string prefix = new string(reader.ReadChars(4));
            if (prefix != "DICM")
            {
                throw new NotImplementedException("Only valid dicom files can by parsed right now");
            }
            
            DFile dicomImage = new DFile();


            //Read the Meta Information
            //  This is seperate because it always has an explicit VR;
            DReader dReader = new DReader(reader,DTransferSyntax.ExplicitLittleEndian);
            dicomImage.MetaInformation = dReader.ReadMetaInformation();


            //The reader should read based on the defined transfersyntax
            //It used to be a static reader, but then you had to give the TS every call
            //I'm not sure what's best yet, but I'll keep it like this now
            dReader.TransferSyntax = dicomImage.TransferSyntax;


            //Start reading dicom Files
            while (reader.BaseStream.Position < reader.BaseStream.Length)
            {
                DDataElement dataElement = dReader.ReadNextDataElement();
                dicomImage.DataElements.Add(dataElement);
            }
            return dicomImage;
        }
        
        internal DFile(){
            //It can't be initialized, because it required to have an file to load
            //I'm pretty sure this is the appropriate way to go. 
        }
    }
}
